<?php
include('../../../../inc/application_settings.php');
include(FILESTORE);
include(IMAGE);

$email = $_COOKIE['EMAIL'];
$fileId = (isset($_REQUEST['FILE_ID'])) ? $_REQUEST['FILE_ID'] : null;
$width = (isset($_REQUEST['WIDTH'])) ? $_REQUEST['WIDTH'] : 150;
$height = (isset($_REQUEST['HEIGHT'])) ? $_REQUEST['HEIGHT'] : 150;
$showActive = (isset($_REQUEST['SHOW_ACTIVE']) && $_REQUEST['SHOW_ACTIVE'] == 1) ? true : false;

$password = Cryptic::decrypt($_COOKIE['PASSWORD'], $email);
$auth = new Authentication($email, $password);


if ($auth->checkAuthentication()){
	if (is_numeric($fileId)){
		$sqlObj = Sql_Object::getInstance();
		$sqlObj->connect();
 		
		
		
		$fs = new Filestore($sqlObj);
		$info = $fs->getFileInfo($fileId);
		
		$image = new Image();
		$image->load($info['path'], $info['physicalname']);
		$image->resize($width, $height);
		
		//create the icon image
		if ($showActive){
			$isActive = isImageActive($fileId, $sqlObj);
			$icon = ($isActive) ? 'success.png' : 'fail.png';
			$iconImage = new Image();
			$iconImage->load(ROOT . '/Biohof/screen/admin/images', $icon);
			
			//add the icon to gallry image
			$posX = $image->getWidth() - 20;
			$posY = $image->getHeight() -20;
			$image->addImage($iconImage, $posX, $posY);
		}
		
		
		$image->display();
		$sqlObj->disconnect();
	}
}


/**
 * checks the active state of image with given file id
 * @param 		int		 $fileId
 * @param 		mixed	 $sqlObj
 * @return 		bool	 $isActive
 */
function isImageActive($fileId, $sqlObj){
	$isActive = false;
	$sql = 'select active from ' . DB_BIO . '.gallery_image where file_id = ?';
	$params = array($fileId);
	
	$sqlObj->execute($sql, $params);
	$row = $sqlObj->fetch();
	$active = $row['active'];
	
	if ($active == 1)
		$isActive = true;
	return $isActive;
}


?>